package com.atguigu.test;

import com.atguigu.RabbitMQProducerMainType;
import org.junit.jupiter.api.Test;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
//@SpringBootTest(classes = RabbitMQProducerMainType.class)
public class ProducerTest_ {
    @Autowired
    RabbitTemplate rabbitTemplate;


    @Test
    public void testSendMessageTTL(){
        //1.创建消息后置处理器对象
        MessagePostProcessor messagePostProcessor = (Message message) ->{

            //设定TTL时间，以毫秒为单位
            message.getMessageProperties().setExpiration("5000");
            return message;
        };

        //2.发送信息
        rabbitTemplate.convertAndSend(
                "","testttl",
                "Hello atguigu",messagePostProcessor
        );
    }

    //==========================================================

    //测试大量信息发送到服务器上
    @Test
    public void testSendMessage(){
        for (int i = 0; i < 100; i++) {
            rabbitTemplate.convertAndSend(
                    "atguigu.exchange.direct",
                    "atguigu.routing.key.good",
                    "Hello atguigu "+i
            );
        }
    }
    //=====================================================================

    public static final String EXCHANGE_DIRECT_ORDER ="exchange.direct.order";
    public static final String ROUTING_KEY = "order";
    public static final String QUEUE_NAME ="queue.order";

    //可靠性保证
    @Test
    public void testSendConfirmAndReturn(){
        //1.测试正确      confirm()执行
        rabbitTemplate.convertAndSend(EXCHANGE_DIRECT_ORDER,ROUTING_KEY,"hello,ok");

        //2.测试失败   交换机名称不对      confirm()执行
        //reply-code =404,reply-text =NOT_FOUND- no exchange 'exchange.direct.order.xxxx' in vhost '/'
//        rabbitTemplate.convertAndSend("exchange.direct.order.xxxx",ROUTING_KEY,"hello,exchange name error");

        //2.测试失败   路由key错误的    confirm()  和 returnedMessage() 都执行
        //消息主体   hello,routing key error
        //应答码：312
        //描述：NO_ROUTE
        //消息使用的交换器  exchange: exchange.direct.order
        //消息使用的路由键 routing: xxxx
//        rabbitTemplate.convertAndSend(EXCHANGE_DIRECT_ORDER,"xxxx","hello,routing key error");

    }
    //========================================================================

    //简单模式下，没有用到交换机，用默认交换机，工作队列模式也一样
//    public static final String EXCHANGE_DIRECT = "";

    //在简单模式下，消息直接放松到队列，此时生产者把队列名称从路由键参数这里传入
    public static final String ROUTING_KEY_SIMPLE = "atguigu.queue.simple";

    public static final String ROUTING_KEY_WORK = "atguigu.queue.work";//用于测试工作队列模式

    public static final String EXCHANGE_FANOUT = "atguigu.exchange.fanout";//用于测试广播模式

    public static final String EXCHANGE_DIRECT = "atguigu.exchange.direct";//测试路由模式（定向类型交换机）

    public static final String EXCHANGE_TOPIC ="atguigu.exchange.topic";//测试主题模式（通配符类型交换机）
    public static final String ROUTING_KEY_ERROR = "#.error";
    public static final String ROUTING_KEY_ORDER = "order.*";
    public static final String ROUTING_KEY_ALL = "*.*";


    //简单消息模式：不需要创建交换机和队列进行绑定，底层采用默认交换机进行消息转发
    //delivery_mode:2   表示消息是持久化的
//    @Test
//    public void testSend(){
//        rabbitTemplate.convertAndSend(EXCHANGE_DIRECT,ROUTING_KEY_SIMPLE,"hello,simple");
//    }

    //工作队列模式：解决消息积压问题，多个消费者共同消费队列的信息，但不能同一条信息,默认轮询
    //delivery_mode:2   表示消息是持久化的
//    @Test
//    public void testSendWork(){
//        for (int i = 1; i <= 100; i++) {
//            rabbitTemplate.convertAndSend(EXCHANGE_DIRECT,ROUTING_KEY_WORK,"hello,work"+i);
//        }
//    }

    //测试广播类型交换机，所有队列都能收到信息，即所有消费者都能收到信息
    @Test
    public void testSendFanout(){
        rabbitTemplate.convertAndSend(EXCHANGE_FANOUT,"","test fanout");

    }

    //测试定向类型交换机，根据发送路由key和绑定路由key进行比较，一致的话队列可以收到信息，不一致收不到消息
    @Test
    public void testSendDirect(){
        //正确  发送的路由key和绑定的路由key一致
//        rabbitTemplate.convertAndSend(EXCHANGE_DIRECT,"atguigu.routing.key.error","test direct error");
        //错误，发送的路由key和绑定的路由key不一致
        rabbitTemplate.convertAndSend(EXCHANGE_DIRECT,"atguigu.routing.key.order","test direct order");




    }

        //测试主题类型交互机    * 匹配不多不少一个单词 # 匹配0个或多个单词

    @Test
    public void testSendTopic(){
//            rabbitTemplate.convertAndSend(EXCHANGE_TOPIC,"order.item.error","test[order.error]");
        rabbitTemplate.convertAndSend(EXCHANGE_TOPIC,"order.item.error","test[order.error]");
    }


}
